package cn.com.dcsgo.controller;

import cn.com.dcsgo.domain.dto.SaleOpportunityModifyDTO;
import cn.com.dcsgo.domain.dto.SaleOpportunitySaveDTO;
import cn.com.dcsgo.domain.po.SaleOpportunity;
import cn.com.dcsgo.domain.result.Result;
import cn.com.dcsgo.domain.vo.SaleOpportunityVO;
import cn.com.dcsgo.service.SaleOpportunityService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;

/**
 * SaleOpportunityController
 *
 * @author Dcsgo
 * @since 14/5/2025 下午 9:19
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("sale/opportunity")
@RequiredArgsConstructor
public class SaleOpportunityController {
    private final SaleOpportunityService saleOpportunityService;

    @PostMapping
    public Result<Serializable> save(@RequestBody SaleOpportunitySaveDTO saleOpportunitySaveDTO) {
        SaleOpportunity saleOpportunity = new SaleOpportunity();
        BeanUtils.copyProperties(saleOpportunitySaveDTO, saleOpportunity);
        log.debug("save saleOpportunity:{}", saleOpportunity);
        saleOpportunityService.save(saleOpportunity);
        return Result.ok();
    }

    @GetMapping("page/{page}/{size}")
    public Result<IPage<SaleOpportunityVO>> page(@PathVariable Integer page,
                                                 @PathVariable Integer size,
                                                 SaleOpportunityVO saleOpportunityVO) {
        Page<SaleOpportunityVO> pageParam = new Page<>(page, size);
        return Result.ok(saleOpportunityService.getPage(pageParam, saleOpportunityVO));
    }

    @DeleteMapping("{saleOpportunityId}")
    public Result<Serializable> remove(@PathVariable Long saleOpportunityId) {
        saleOpportunityService.removeById(saleOpportunityId);
        return Result.ok();
    }

    @PutMapping("{saleOpportunityId}")
    public Result<Serializable> modify(@PathVariable Long saleOpportunityId,
                                       @RequestBody SaleOpportunityModifyDTO saleOpportunityModifyDTO) {
        SaleOpportunity saleOpportunity = new SaleOpportunity();
        saleOpportunity.setId(saleOpportunityId);
        log.debug("saleOpportunityId:{}", saleOpportunityId);
        BeanUtils.copyProperties(saleOpportunityModifyDTO, saleOpportunity);
        log.debug("saleOpportunity:{}", saleOpportunity);
        saleOpportunityService.updateById(saleOpportunity);
        return Result.ok();
    }
}
